﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows.Input;
using GalaSoft.MvvmLight.CommandWpf;
using LiSAClient.Core;
using LiSAClient.Core.Services;
using LiSAClient.Core.Services.Factories;
using LiSAClient.Core.Viewmodel;
using LiSAClient.LiSAService;

namespace LiSAClient.WpfClient.ViewModel.Projects
{
    class ProjectsOverviewViewModel : ContentViewModel
    {
        #region Observable Objects

        private List<ProjectShortInfo> projects;
        public List<ProjectShortInfo> Projects
        {
            get
            {
                return this.projects;
            }

            set
            {
                this.Set(() => this.Projects, ref this.projects, value);
            }
        }

        private ProjectShortInfo selectedProjectShortInfo;
        public ProjectShortInfo SelectedProjectShortInfo
        {
            get
            {
                return this.selectedProjectShortInfo;
            }

            set
            {
                this.Set(() => this.SelectedProjectShortInfo, ref this.selectedProjectShortInfo, value);
            }
        }


        public ICommand ShowProject { get; private set; }
        #endregion

        #region Initializers
        /// <summary>
        /// Initializes the components
        /// </summary>
        public ProjectsOverviewViewModel()
        {
            this.Projects = new List<ProjectShortInfo>();
            this.ShowProject = new RelayCommand(this.DoShowProject);
        }

        /// <summary>
        /// Retrieves the relevant information for the viewmodel
        /// </summary>
        public override void OnPresented()
        {
            this.Presenter.StartWaiting("Fetching your projects...");
            Task.Factory.StartNew(
                () =>
                {
                    // Retrieve projects
                    this.Projects = ServiceFactory.Instance.Get<ProjectService>().GetProjectShortinfos(AppContext.CurrentContext.User.ID);
                 
                    this.Presenter.FinishedWaiting();
                });
        }

        #endregion

        #region Methods
        /// <summary>
        /// Handles displaying information for the selected project
        /// </summary>
        public void DoShowProject()
        {
            this.Presenter.PresentContent(new ProjectDetailsViewModel(this.SelectedProjectShortInfo.ProjectID), ShowInWindow = true);
        }
        #endregion
    }
}
